package day03;
/*
@Author: 王少杰
@Date: 2025/1/2
@Time: 9:56
@FileName: Demo03.java
@描述:
构造方法/初始化方法:
    python __init__ 创建对象时自动调用的。

    构造方法与类名同名的方法
    不能有返回值
    定义类的时候，没有显示定义构造方法，系统会自动生成一个无参的构造方法
    定义类的时候，显示了定义构造方法，系统不会自动生成无参的构造方法
    构造方法支持重载。创建对象更加灵活。

toString() Object
    将对象转成字符串
    如果类中没有定义toString()，会使用Object类中定义的toString()方法
    如果类中定义toString()了，会使用本类定义的toString()。
    println()时会自动调用
*/
public class Demo03 {
    public static void main(String[] args) {
        Book book = new Book("西游记","吴承恩");
        System.out.println(book);

        Book book1 = new Book("西游记","清华大学出版社","吴承恩",26);
        System.out.println(book1);

        Book1 book2 = new Book1();
        book2.bookName = "西游记";
        book2.author = "吴承恩";
        book2.press = "清华大学出版社";
        book2.price = 30;
        System.out.println(book2);
    }
}
//属性:书名，出版社，作者，价格
class  Book{
    String bookName;
    String press;
    String author;
    float price;

    public Book(String bookName, String press, String author, float price) {
        this.bookName = bookName;
        this.press = press;
        this.author = author;
        this.price = price;
    }


    public Book(String bookName, String author) {
        //成员变量和局部变量同名时，this用来区分成员变量和局部变量
        this.bookName = bookName;
        this.author = author;
    }

    @Override
    public String toString() {
        return "Book{" +
                "bookName='" + bookName + '\'' +
                ", press='" + press + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                '}';
    }
}

class Book1{
    String bookName;
    String press;
    String author;
    float price;

    @Override
    public String toString() {
        return "Book1{" +
                "bookName='" + bookName + '\'' +
                ", press='" + press + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                '}';
    }
}